package switchviz.core.mib;

import java.awt.BorderLayout;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.apache.log4j.Logger;
import org.jdom.JDOMException;

import switchviz.core.ResourceManager;
import switchviz.gui.component.IListener;
import switchviz.gui.component.JLabeldComponent;
import switchviz.gui.component.JMaskedTextField;
import switchviz.gui.component.PaddingPanel;

public class MIBConfigurationPanel extends PaddingPanel
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2435266466214840901L;
	private static Logger logger = Logger.getLogger(MIBConfigurationPanel.class);
	private JLabeldComponent _txtdescr;
	private JMaskedTextField _txtrangelow;
	private JMaskedTextField _txtrangehigh;
	private JLabeldComponent _txttype;
	private JLabeldComponent _txtname;
	private JLabeldComponent _txtoid;
	private JLabeldComponent _cbportrequired;
	private JButton _buttapply;
	private JButton _butttest;
	private JButton _buttsnmpwalk;
	private MIBDataModel _datamodel;
	
	public void setValues(MIBDataModel datamodel)
	{
		_datamodel = datamodel;
		this.reloadData();
	}
	
	public void setTestEnable(boolean b)
	{
		_butttest.setEnabled(b);
	}
	
	public MIBDataModel getValues()
	{
		_datamodel.setName(_txtname.getText());
		_datamodel.setOID(_txtoid.getText());
		_datamodel.setType(_txttype.getText());
		_datamodel.setDescription(_txtdescr.getText());
		_datamodel.setRangeLow(Long.valueOf(_txtrangelow.getText()));
		_datamodel.setRangeHigh(Long.valueOf(_txtrangehigh.getText()));
		_datamodel.setPortRequired(_cbportrequired.getCheckBox().isSelected());
		return _datamodel;
	}
	
	public MIBConfigurationPanel()
	{
		try 
		{
			initialize();
		} 
		catch (Exception e) { logger.error(e.getMessage()); }
	}

	public void addListener(IListener l)
	{
		_buttapply.addActionListener(l);
		_butttest.addActionListener(l);
		_buttsnmpwalk.addActionListener(l);
	}
	
	private void reloadData()
	{
		_txtname.setText(getDataModel().getName());
		_txtoid.setText(getDataModel().getOID());
		_txttype.setText(getDataModel().getType());
		_txtdescr.setText(getDataModel().getDescription());
		_txtrangehigh.setText(String.valueOf(getDataModel().getRangeHigh()));
		_txtrangelow.setText(String.valueOf(getDataModel().getRangeLow()));
		_cbportrequired.getCheckBox().setSelected(getDataModel().getPortRequired());
		_txtdescr.getTextArea().setCaretPosition(0);
	}
	
	private void initialize() throws JDOMException, IOException
	{

		_txtdescr = new JLabeldComponent("Description: ", JLabeldComponent.TEXTAREA);
        
        _buttapply = new JButton("Apply Changes");
        _buttapply.setActionCommand(ResourceManager.getInstance().getActionChange());
		_butttest = new JButton("Test OID");
		_butttest.setActionCommand(ResourceManager.getInstance().getActionTest());
        _butttest.setEnabled(false);
        _buttsnmpwalk = new JButton("SNMP Walk");
        _buttsnmpwalk.setActionCommand(ResourceManager.getInstance().getActionStart());
		
        _txtname = new JLabeldComponent("Name: ", JLabeldComponent.TEXTFIELD);
        _txtname.getTextField().setColumns(15);
        _txtoid = new JLabeldComponent("OID: ", JLabeldComponent.TEXTFIELD);
        _txtoid.getTextField().setColumns(15);
        _txtrangehigh = new JMaskedTextField("Range Limit High: ", JMaskedTextField.INTNUM);
        _txtrangehigh.setText("0");
        _txtrangelow = new JMaskedTextField("Range Limit Low: ", JMaskedTextField.INTNUM);
        _txtrangelow.setText("0");
        _txttype = new JLabeldComponent("Type: ", JLabeldComponent.TEXTFIELD);
        _txttype.getTextField().setColumns(15);
        _cbportrequired = new JLabeldComponent("Port Required: ", JLabeldComponent.CHECKBOX);
        
        JPanel buttonpanel = new JPanel();
        buttonpanel.setLayout(new BoxLayout(buttonpanel, BoxLayout.LINE_AXIS));
        buttonpanel.add(Box.createHorizontalGlue());
        buttonpanel.add(_buttapply);
        buttonpanel.add(_butttest);
        buttonpanel.add(_buttsnmpwalk);
        
        PaddingPanel confpane = new PaddingPanel();
        
        confpane.setLayout(new BoxLayout(confpane, BoxLayout.PAGE_AXIS));
        confpane.add(_txtname);
        confpane.add(_txtoid);
        //confpane.add(_txttype);
        //confpane.add(_txtrangelow);
        //confpane.add(_txtrangehigh);
        confpane.add(_txtdescr);
        confpane.add(_cbportrequired);
        confpane.add(new JLabel(" "));
        confpane.setBorder(BorderFactory.createLoweredBevelBorder());
        
        this.setLayout(new BorderLayout());
        this.add(confpane, BorderLayout.CENTER);
        this.add(buttonpanel, BorderLayout.SOUTH);
	}
	
	private MIBDataModel getDataModel()
	{
		if (_datamodel == null)
		{
			_datamodel = new MIBDataModel();
		}
		return _datamodel;
	}
}
